// pages/mine/opinion/opin.js
import { HTTP } from '../../../utils/http.js'
let ajax = new HTTP()
Page({
    data: {
        maxLength: 200,
        src: []
    },
    // 选择图片
    chooseImgTap() {
        var that = this
        wx.chooseImage({
            count: 3,
            success: function(res) {
                // 成功个数
                var successUp = 0
                // 失败个数
                var failUp = 0
                // 总共个数
                var length = res.tempFilePaths.length
                // 第几个
                var i = 0
                wx.showToast({
                    title: '上传中',
                    icon: 'loading',
                    mask: true
                })
                that.uploadDIY(res.tempFilePaths, successUp, failUp, i, length)
            }
        })
    },
    // 上传图片
    uploadDIY(filePaths, successUp, failUp, i, length) {
        wx.uploadFile({
            url: 'https://www.xlroader.com/index.php/v1/bbsarticle/uploadfile',
            filePath: filePaths[i],
            name: 'pic',
            success: (res) => {
                successUp++
            },
            fail: (res) => {
                failUp++
            },
            complete: (res) => {
                this.data.src.push(JSON.parse(res.data).data.url)
                i++
                if (i == length) {
                    this.setData({
                        src: this.data.src
                    })
                    wx.hideToast()
                    setTimeout((res) => {
                        wx.showToast({
                            title: '上传成功',
                            icon: 'success',
                            mask: true,
                            duration: 800
                        })
                    }, 500)
                } else { //递归调用uploadDIY函数
                    this.uploadDIY(filePaths, successUp, failUp, i, length)
                }
            },
        })
    },
    // 预览图片
    previewImgTap(event) {
        var that = this
        let index = event.currentTarget.dataset.index
        wx.previewImage({
            urls: that.data.src,
            current: that.data.src[index]
        })
    },
    // 删除图片
    delImage(event) {
        let that = this
        let index = event.currentTarget.dataset.index
        wx.showModal({
            title: '',
            content: '是否删除图片',
            success(res) {
                if (res.confirm) {
                    that.data.src.splice(index, 1)
                    that.setData({
                        src: that.data.src
                    })
                }
            }
        })
    },
    // 表单提交
    bindFormSubmit(event) {
        let submitArr = event.detail.value
        if (submitArr.content === "") {
            wx.showToast({
                title: '反馈内容不能为空！',
                icon: "none"
            })
            return
        }
        ajax.request({
            method: "post",
            url: "suggestion/save",
            data: {
                memberid: wx.getStorageSync("memberid"),
                pic: this.data.src,
                content: submitArr.content
            },
            success: (res) => {
                if (res.rescode == "0000") {
                    wx.showToast({
                        title: '您的反馈已收到',
                        icon: "success",
                        duration: 1500,
                        mask: true,
                        success: (res) => {
                            setTimeout((res) => {
                                wx.switchTab({
                                    url: '/pages/mine/mine',
                                })
                            }, 1500) 
                        }
                    })
                }
            }
        })
    }
})